﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PluginsAPI;

namespace BleQua {
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window {
		public MainWindow( ) {
			InitializeComponent( );
			LoadStatus( );
		}

		#region Bind events
		private void M_Options_Click(object sender, RoutedEventArgs e) {
			Windows.OptionsWindows optionsWindow = new Windows.OptionsWindows( );
			optionsWindow.Show( );
		}

		private void M_LogOut_Click(object sender, RoutedEventArgs e) {
			Windows.Dialogs.LoginWindow loginWindow = new Windows.Dialogs.LoginWindow( );
			this.Hide( );
			loginWindow.Closed += new EventHandler(Login);
			loginWindow.ShowDialog( );
		}

		private void M_ReStart_Click(object sender, RoutedEventArgs e) {
			// Get the parameters/arguments passed to program if any
			string arguments = string.Empty;
			string[ ] args = Environment.GetCommandLineArgs( );
			for (int i = 1; i < args.Length; i++) // args[0] is always exe path/filename
				arguments += args[i] + " ";

			//because args[0] in Debug = "D:\\VSRepo\\BleQua\\BleQua .NET\\bin\\Debug\\BleQua.vshost.exe" - so we remove .vshost.
			string programName = args[0].Replace(".vshost.", ".");

			// Restart current application, with same arguments/parameters
			Application.Current.Shutdown( );
			System.Diagnostics.Process.Start(programName, arguments);
		}

		private void M_ShutDown_Click(object sender, RoutedEventArgs e) {
			this.Close( );
		}

		private void M_TransferHistory_Click(object sender, RoutedEventArgs e) {
			Windows.TransferHistoryWindow transferHistoryWindow = new Windows.TransferHistoryWindow( );
			transferHistoryWindow.Show( );
		}

		private void M_TalkHistory_Click(object sender, RoutedEventArgs e) {
			Windows.TalkHistoryWindow talkHistoryWindow = new Windows.TalkHistoryWindow( );
			talkHistoryWindow.Show( );
		}

		private void M_AddContact_Click(object sender, RoutedEventArgs e) {
			Windows.Dialogs.AddContactWindow addContactWindow = new Windows.Dialogs.AddContactWindow( );
			addContactWindow.Show( );
		}

		private void M_SearchFriends_Click(object sender, RoutedEventArgs e) {
			Windows.Dialogs.SearchFriendsWindow searchFriendsWindow = new Windows.Dialogs.SearchFriendsWindow( );
			searchFriendsWindow.Show( );
		}

		private void M_Find_Click(object sender, RoutedEventArgs e) {
			LoadStatus( );
		}

		private void M_ImortExport_Click(object sender, RoutedEventArgs e) {
			Windows.Dialogs.ImortExportWindow importExportWindow = new Windows.Dialogs.ImortExportWindow( );
			importExportWindow.Show( );
		}

		private void M_AddGroup_Click(object sender, RoutedEventArgs e) {
			if (App.Core.MyService.Count != 0) {
				App.Core.MyService[0].Login = "24000335";
				App.Core.MyService[0].Password = "123456";
				App.Core.MyService[0].LogIn( );
			}
		}

		private void M_USGroups_Click(object sender, RoutedEventArgs e) {

		}

		private void M_Refresh_Click(object sender, RoutedEventArgs e) {

		}

		private void M_Update_Click(object sender, RoutedEventArgs e) {
			Windows.Dialogs.UpdateWindow updateWindow = new Windows.Dialogs.UpdateWindow( );
			updateWindow.Show( );
		}

		private void M_IndexHelp_Click(object sender, RoutedEventArgs e) {
			Windows.HelpWindow helpWindow = new Windows.HelpWindow( );
			helpWindow.Show( );
		}

		private void M_About_Click(object sender, RoutedEventArgs e) {
			Windows.Dialogs.AboutWindow aboutWindow = new Windows.Dialogs.AboutWindow( );
			aboutWindow.Show( );
		}
		private void Window_Closed(object sender, EventArgs e) {
			App.Core.Dispose( );
			App.Current.Shutdown( );
		}
		#endregion

		#region Processing
		public void LoadStatus( ) {
			this.SB_Status.Items.Clear( );//.TB_Status
			if (App.Core.MyService.Count >= 1) {
				Menu _Menu = new Menu( );
				int i = 0;
				foreach (INetworkPlugin Serv in App.Core.MyService) {
					_Menu.Items.Add(GenerateMenu(Serv, i));
					i++;
				}
				this.SB_Status.Items.Add(_Menu);//.TB_Status
			}
		}

		MenuItem GenerateMenu(INetworkPlugin S, int i) {
			MenuItem MI = new MenuItem( );
			MI.Name = "NetPlug_" + i + "_S";
			MI.ToolTip = S.Login;
			MI.Header = S.Name;
			MI.Height = 20;
			MI.Items.Add(new MenuItem( ) { Header = "Connected" });
			MI.Items.Add(new MenuItem( ) { Header = "Free" });
			MI.Items.Add(new Separator( ));
			MI.Items.Add(new MenuItem( ) { Header = "Away" });
			MI.Items.Add(new MenuItem( ) { Header = "XAway" });
			MI.Items.Add(new MenuItem( ) { Header = "DND" });
			MI.Items.Add(new Separator( ));
			MI.Items.Add(new MenuItem( ) { Header = "Invisible" });
			MI.Items.Add(new MenuItem( ) { Header = "Disconnected" });
			MI.Items.Add(new Separator( ));
			MI.Items.Add(new MenuItem( ) { Header = "Change desc" });
			return MI;

			// Dynamic handler ;D
			//MenuItem es = new MenuItem();
			//es.Click += new RoutedEventHandler((sender, e) => App.Core.MyService[1].SendMessage(e.ToString(), (string)sender));

		}
		#endregion

		#region Functions
		private void Login(object sender, EventArgs e) {
			this.Show( );
		}
		#endregion
	}
}